/*
	Copyright (c) 2010 <copyright holders>
	This source code is licensed under The MIT License,
	see the file LICENSE for details.
	*/

#ifndef SERVER_HPP
#define SERVER_HPP

#include <boost/asio/ip/tcp.hpp>
#include <map>

#include "Connection.hpp"
#include "Net.hpp"

namespace spelprog2 {
namespace net {

class Server : public Net {
    HIDE_EVIL_CONSTRUCTORS(Server);

    typedef boost::asio::ip::tcp::acceptor Acceptor;
    typedef std::map<UserId, Connection::Pointer> PeerMap;

    Acceptor acceptor;
    PeerMap peers;
    MessageListenerRegistry<NetMessageInfo> internalRegistry_;
    bool hasLocal_;

    void removePeer(UserId id);
    void startAccept();
    void handleAccept(Connection::Pointer conn, const boost::system::error_code & e);
public:
    Server(unsigned port, const std::string & password, bool hasLocalUser = false, const std::string & username = "");
    ~Server();

    // Implementations of super methods =======================================

    virtual UserId getMyClientId() const;

    virtual void sendToUser(const Message & msg, UserId receiver);
    virtual void sendToAll(const Message & msg);
};

}; //namespace net
}; //namespace spelprog2

#endif //SERVER_HPP
